package com.wyr.common.vo;

import com.wyr.common.enums.HttpCodeEnum;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import org.slf4j.MDC;

/**
 * 统一结果集处理类
 *
 * @author wangyiren
 * @version 1.0
 * &#064;title JsonResult
 * &#064;description
 * &#064;date 2025/03/07 09:04
 */
@Data
@Schema(description = "统一结果集处理类")
public class Result<T> {

    private static final String TRACE_ID = "traceId";

    /**
     * 成功标志
     */
    @Schema(description = "成功标志")
    private boolean success = true;

    /**
     * 返回处理消息
     */
    @Schema(description = "返回处理消息")
    private String message = "操作成功！";

    /**
     * 返回代码
     */
    @Schema(description = "返回代码")
    private Integer code = 0;

    /**
     * 返回数据对象 data
     */
    @Schema(description = "返回数据对象")
    private T result;

    /**
     * traceId
     */
    @Schema(description = "traceId")
    private String traceId = MDC.get(TRACE_ID);

    /**
     * timestamp
     */
//    @Schema(description = "timestamp")
//    private long timestamp = System.currentTimeMillis();

    public Result() {

    }

    public Result<T> success(String message) {
        this.message = message;
        this.code = HttpCodeEnum.SUCCESS.getCode();
        this.success = true;
        return this;
    }


    public static Result<Object> ok() {
        Result<Object> r = new Result<>();
        r.setSuccess(true);
        r.setCode(HttpCodeEnum.SUCCESS.getCode());
        r.setMessage("成功");
        return r;
    }

    public static Result<Object> ok(String msg) {
        Result<Object> r = new Result<>();
        r.setSuccess(true);
        r.setCode(HttpCodeEnum.SUCCESS.getCode());
        r.setMessage(msg);
        return r;
    }

    public static Result<Object> ok(Object data) {
        Result<Object> r = new Result<>();
        r.setSuccess(true);
        r.setCode(HttpCodeEnum.SUCCESS.getCode());
        r.setResult(data);
        return r;
    }

    public static Result<Object> ok(String msg, Object data) {
        Result<Object> r = ok(data);
        r.setMessage(msg);
        return r;
    }

    public static Result<Object> error(String msg) {
        return error(HttpCodeEnum.ERROR.getCode(), msg);
    }

    public static Result<Object> error(Integer code, String msg) {
        Result<Object> r = new Result<>();
        r.setCode(code);
        r.setMessage(msg);
        r.setSuccess(false);
        return r;
    }

    public Result<T> error500(String message) {
        this.message = message;
        this.code = HttpCodeEnum.ERROR.getCode();
        this.success = false;
        return this;
    }

}
